package top.tangyh.lamp.care.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.tangyh.basic.exception.BizException;
import top.tangyh.lamp.care.mapper.BPatEventMapper;
import top.tangyh.lamp.care.service.BPatEventService;
import top.tangyh.basic.base.service.impl.SuperServiceImpl;
import top.tangyh.lamp.care.manager.BPatEventManager;
import top.tangyh.lamp.care.entity.BPatEvent;
import top.tangyh.lamp.care.vo.save.BPatEventSaveVO;
import top.tangyh.lamp.care.vo.save.EventInputVO;
import top.tangyh.lamp.care.vo.update.BPatEventUpdateVO;
import top.tangyh.lamp.care.vo.result.BPatEventResultVO;
import top.tangyh.lamp.care.vo.query.BPatEventPageQuery;
import top.tangyh.lamp.common.constant.SpecialEventEnum;
import top.tangyh.lamp.common.utils.PDateUtils;

import java.util.List;

/**
 * <p>
 * 业务实现类
 * 特殊事件表
 * </p>
 *
 * @author zqb
 * @date 2024-07-22 16:36:14
 * @create [2024-07-22 16:36:14] [zqb] [代码生成器生成]
 */
@Slf4j
@RequiredArgsConstructor
@Service
@Transactional(readOnly = true)
public class BPatEventServiceImpl extends SuperServiceImpl<BPatEventManager, Long, BPatEvent> implements BPatEventService {

    private  final BPatEventMapper bPatEventMapper;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addEvent(EventInputVO eventInputVO) {
        String eventName = StringUtils.deleteWhitespace(eventInputVO.getEventName());
        String hospitalNumber = eventInputVO.getHospitalNumber();
        // 判断添加的事件是否为"死亡"事件，如果是就不能重复添加
        if (SpecialEventEnum.DIE.getEventName().equals(eventName)) {
            BPatEvent BPatEvent = bPatEventMapper.selectOneByEventName(eventName, hospitalNumber);
            if (BPatEvent != null) {
                throw new BizException("无法重复添加死亡事件");
            }
        }

        BPatEvent BPatEvent = new BPatEvent();
        BeanUtil.copyProperties(eventInputVO, BPatEvent);
        BPatEvent.setEventTime(PDateUtils.getDateTimeOfTimestamp(eventInputVO.getEventTime()));
        bPatEventMapper.insert(BPatEvent);
    }

    @Override
    public List<BPatEvent> queryEvent(String hospitalNumber) {
        if (StringUtils.isBlank(hospitalNumber)) {
            throw new BizException("病人识别号为空");
        }
        List<BPatEvent> eventList = bPatEventMapper.selectByHospitalNumber(hospitalNumber);
        return eventList;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateEvent(EventInputVO eventInputVO) {
        String eventName = StringUtils.deleteWhitespace(eventInputVO.getEventName());
        String hospitalNumber = eventInputVO.getHospitalNumber();
        bPatEventMapper.updatePatEvetById(eventInputVO, eventName);
        List<BPatEvent> list = bPatEventMapper.selectByEventName(SpecialEventEnum.DIE.getEventName(), hospitalNumber);
        if (list.size() > 1) {
            throw new BizException("无法重复添加死亡事件");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteEvent(Long id) {
        if (id == null) {
            throw new BizException("主键不能为空");
        }
        bPatEventMapper.deleteById(id);
    }

    @Override
    public List<BPatEvent> selectEventListByParam(String hospitalNumber, String startTime, String endTime,String icuCode) {
        return bPatEventMapper.selectEventListByParam(hospitalNumber,startTime,endTime,icuCode);
    }

    @Override
    public List<BPatEvent> selectEventListForBloodPressure(String hospitalNumber, String startTime, String endTime) {
        return bPatEventMapper.selectEventListForBloodPressure(hospitalNumber,startTime,endTime);
    }
}


